#pragma once

#include <ddblk/apds.h>
/* 用于初始化apds */
/* 实际不会用到fetch */
#include <dinfra/mpwf/wmsp.h>

/* 注意：使用巨页会增加rss */
/* 使用wmsp */
#define POOL_SIZE (2 * 1024 * 1024)
static TLS_INITIAL wmobj_t *malloc_perm_pool_list;
DEF_WMSP_OBJ_CACHED(malloc_perm_pool, &malloc_perm_pool_list, HDEF_CACHE_LINE_BYTES, POOL_SIZE)
// DEF_WMSP_OBJ_CACHED_HUGEPAGE(malloc_perm_pool, &malloc_perm_pool_list, HDEF_CACHE_LINE_BYTES, POOL_SIZE, SIZE_2M, mark_hugepage)
/* 变成自己的size */
SIN void *mlcc_palloc(size_t size){ return malloc_perm_pool_palloc(size); }

DEF_LOCKNODE_CUSTOM(wmsplocknode, HDEF_CACHE_LINE_BYTES, malloc_perm_pool_cachealloc, malloc_perm_pool_cachefree)
// SAI void *get_locknode(const size_t size){ if (size <= objsize) return (void *)objname##_cachealloc(); return NULL; }\
// SAI void ret_locknode(void *locknode, const size_t size){ PARAM_USED(size); objname##_cachefree((spobj_t*)locknode); }\
// NEW_OUFUN(lknode, wmsplocknode, get_locknode, ret_locknode)


// #define LOCK_MAXFUTURES	(1)
// static TLS_INITIAL apdsfuture_t apdsfuture_tls[LOCK_MAXFUTURES]; 
// static TLS_INITIAL hword_t apdsfuture_cnt; 
// static TLS_INITIAL slist_t *apdsfuture_head;
// SAI apdsfuture_t *apfsdsfuture_get_tls(){
// 	if (!apdsfuture_head){
// 		ASSERT(apdsfuture_cnt < LOCK_MAXFUTURES);
// 		return &apdsfuture_tls[apdsfuture_cnt++];
// 	}
// 	slist_t *ret = apdsfuture_head;
// 	apdsfuture_head = ret->next;
// 	return (apdsfuture_t *)ret;
// }
// SAI void apfsdsfuture_ret_tls(apdsfuture_t *future){
// 	slist_t *node = (slist_t *)future;
// 	node->next = apdsfuture_head;
// 	apdsfuture_head = node;
// }
SAI apdsfuture_t *apfsdsfuture_get_tls(){
	return NULL;
}
SAI void apfsdsfuture_ret_tls(apdsfuture_t *future){
	PARAM_USED(future);
}
LOCKS2APDS(mutex, apdsmutex, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)
LOCKS2APDS(pspin, apdspspin, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)

LOCKS2APDS(spin, apdsspin, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)
LOCKS2APDS(ticket, apdsticket, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)
LOCKS2APDS(futex_spin, futex_apdsspin, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)
LOCKS2APDS(futex_ticket, futex_apdsticket, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)
LOCKS2APDS(yield_spin, yield_apdsspin, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)
LOCKS2APDS(yield_ticket, yield_apdsticket, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)


/* 声明依赖 */
SAI apdstinfo_t *apdstls_get();
SAI void apdstls_upd();
NEW_OUFUN(apdstinfo, mtls_apdstinfo, apdstls_get, apdstls_upd);


DEF_COMBINE(ccsync, ccsync, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode))
DEF_COMBINE(dsmsync, dsmsync, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode))

/* 先定义 hsync */
/* DEF_GHSYNC_STATE(ghsync_state, APDS_NUMA_NR, yield_spin)
DEF_GHSYNC(ghsync, APDS_NUMA_NR, APDS_GET_NUMANODES, yield_spin, &hsync_state.hsync_glk, OUFUN_OF(hsync_state))
DEF_COMBINE(ghsync, ghsync, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode)) */
DEF_HSYNC(hsync, APDS_NUMA_NR, APDS_GET_NUMANODES, spin)
DEF_COMBINE(hsync, hsync, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode))

/* flat */
DEF_COMBINE(flat, flat, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode))
DEF_COMBINE(scmbn, scmbn, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode))
DEF_COMBINE(sffwd, sffwd, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode))

DEF_COMBINE(cacccsyn, cacccsyn, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode))
DEF_COMBINE(cacdsmsyn, cacdsmsyn, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode))


DEF_CACHSYN(cachsyn, APDS_NUMA_NR, APDS_GET_NUMANODES, spin)
DEF_COMBINE(cachsyn, cachsyn, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode))

DEF_COMBINE(cacflat, cacflat, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode))

DEF_COMBINE(cacsffwd, cacsffwd, OUFUN_OF(mtls_apdstinfo), OUFUN_OF(spin_wait), OUFUN_OF(wmsplocknode))

// DEF_LOCKNODE_ALIGNMALLOC(alignlknode, HDEF_CACHE_LINE_BYTES)
// DEF_NLOCK(clh, clh, OUFUN_OF(spin_wait), OUFUN_OF(alignlknode))
// DEF_NLOCK(clh, bounded_clh, OUFUN_OF(spin_wait_bounded), OUFUN_OF(alignlknode))
// DEF_NLOCK(clh, futex_clh, OUFUN_OF(futex_wait), OUFUN_OF(alignlknode))
// DEF_NLOCK(clh, yield_clh, OUFUN_OF(schedyield_wait), OUFUN_OF(alignlknode))

// DEF_NLOCK(mcs, mcs, OUFUN_OF(spin_wait), OUFUN_OF(alignlknode))
// DEF_NLOCK(mcs, bounded_mcs, OUFUN_OF(spin_wait_bounded), OUFUN_OF(alignlknode))
// DEF_NLOCK(mcs, futex_mcs, OUFUN_OF(futex_wait), OUFUN_OF(alignlknode))
// DEF_NLOCK(mcs, yield_mcs, OUFUN_OF(schedyield_wait), OUFUN_OF(alignlknode))

// LOCKS2APDS(clh, apdsclh, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)
// LOCKS2APDS(mcs, apdsmcs, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)
// LOCKS2APDS(futex_clh, futex_apdsclh, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)
// LOCKS2APDS(futex_mcs, futex_apdsmcs, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)
// LOCKS2APDS(yield_clh, yield_apdsclh, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)
// LOCKS2APDS(yield_mcs, yield_apdsmcs, apfsdsfuture_get_tls, apfsdsfuture_ret_tls)

